<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧装饰区域 -->
      <div class="login-decoration">
        <div class="book-stack">
          <div class="book book1"></div>
          <div class="book book2"></div>
          <div class="book book3"></div>
        </div>
        <h1 class="system-title">书籍推荐系统</h1>
        <p class="system-desc">发现您的下一本好书</p>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-form-container">
        <el-card class="login-card" :body-style="{ padding: '30px' }">
          <template #header>
            <div class="form-header">
              <h2>欢迎回来</h2>
              <p>请登录您的账号</p>
            </div>
          </template>
          
          <el-form 
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            label-position="top"
          >
            <el-form-item prop="userName">
              <el-input 
                v-model="loginForm.userName"
                placeholder="用户名"
                :prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                :prefix-icon="Lock"
                show-password
                @keyup.enter="handleLogin"
              />
            </el-form-item>

            <el-form-item>
              <el-button 
                type="primary" 
                :loading="loading"
                class="login-button"
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <span>还没有账号？</span>
            <el-button 
              link 
              type="primary" 
              @click="goToRegister"
            >
              立即注册
            </el-button>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const authStore = useAuthStore()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  userName: '',
  password: ''
})

const rules = {
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const success = await authStore.loginUser(loginForm)
        if (success) {
          ElMessage.success('登录成功')
          router.push('/') // 登录成功后跳转到首页
        } else {
          ElMessage.error('用户名或密码错误')
        }
      } catch (error) {
        ElMessage.error('登录失败，请稍后重试')
        console.error('Login error:', error)
      } finally {
        loading.value = false
      }
    }
  })
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8eb 100%);
}

.login-box {
  display: flex;
  width: 900px;
  height: 500px;
  background: #fff;
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-decoration {
  flex: 1;
  background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  position: relative;
}

.book-stack {
  margin-bottom: 30px;
  position: relative;
  height: 120px;
  width: 100px;
}

.book {
  position: absolute;
  height: 30px;
  width: 100px;
  background: white;
  border-radius: 3px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease;
}

.book1 {
  bottom: 0;
  background: #FFE0B2;
  transform: rotate(-5deg);
}

.book2 {
  bottom: 35px;
  background: #BBDEFB;
  transform: rotate(5deg);
}

.book3 {
  bottom: 70px;
  background: #C8E6C9;
  transform: rotate(-3deg);
}

.book-stack:hover .book {
  transform: rotate(0);
}

.system-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
  text-align: center;
}

.system-desc {
  font-size: 16px;
  opacity: 0.9;
}

.login-form-container {
  flex: 1;
  padding: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 100%;
  border: none;
  box-shadow: none;
}

.form-header {
  text-align: center;
  margin-bottom: 20px;
}

.form-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}

.form-header p {
  margin: 10px 0 0;
  color: #909399;
  font-size: 14px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.form-footer {
  margin-top: 20px;
  text-align: center;
  color: #606266;
}

.form-footer .el-button {
  padding-left: 5px;
  padding-right: 5px;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    flex-direction: column;
    height: auto;
  }

  .login-decoration {
    padding: 30px;
  }

  .login-form-container {
    padding: 20px;
  }
}
</style> 